/**
 * 水果基类
 */
class fruit extends BaseComp{
    private img_fruit:eui.Image;
    private fruit_left:eui.Image;
    private fruit_right:eui.Image;
    private img_di:eui.Image;
    private img_boom:eui.Image;
    private boom_hide:eui.Image;
    private img_kac:eui.Image;
    
    private dot_mc:egret.MovieClip;
    private img_dot:eui.Rect;

    private speedY;
    private speedX;
    private rotationFlag;
    private a;//加速度

    public img_knife:eui.Image;//三发icon
    public img_dragon:eui.Image;//屠龙icon
    
    constructor(data?)
    {
        super('fruitView',data);
    }

    public init()
    {
        this.setAnchor();
        this.setUI();
        this.cacheAsBitmap = true;
        this.touchEnabled = false;
    }

    public get Data()
    {
        return this.data;
    }

    //切水果游戏用
    public born():void
	{
        this.a = 0.3;
        this.speedY = 20 + Math.random()*6;
        this.speedX = Math.random() > 0.5 ? 1 : -1;
        this.rotation = 45 - 90 * Math.random();
        this.rotationFlag = this.rotation > 0 ? 1 : -1;
	}

    //无尽模式用
    public born2(flag)
    {
        this.a = 0.3;
        this.speedY = 7 + Math.random()*6;
        this.speedX = (2 + (Math.random() * 3 + 3)) * flag;
        this.rotation = 45 - 90 * Math.random();
        this.rotationFlag = this.rotation > 0 ? 1 : -1;
    }

    public setUI(data?)
    {
        if(data)
        {   
            this.data = data;
        }
        
        let id = this.data.id;
        let isLight = this.data.isLight;
        let img_source = '';
        if(isLight)
        {
            img_source = 'fruit_light_' + id;
        }
        else
        {
            img_source = 'fruit_full_' + id;
        }
        
        this.img_knife.visible = false;
        this.setScale(0.9);
        let isRandomThreeKnife = GameManager.instance.randomThreeKnife;
        let isRandomDragon = GameManager.instance.randomDragon;
        if(isRandomThreeKnife)
        {
            this.img_knife.visible = true;
            img_source = 'fruit_light_' + id;
            GameManager.instance.randomThreeKnife = false;
        }
        else if(isRandomDragon)
        {
            this.img_dragon.visible = true;
            img_source = 'fruit_light_' + id;
            GameManager.instance.randomDragon = false;
        }
        this.img_fruit.source = img_source;
        this.img_fruit.visible = true;
    }

    public randomRarotion()
    {
        let rotation1 = Math.random() * 180;
        let rotation2 = - 1 * Math.random() * 360;
        let self = this;
        let onComplete1:Function = function(){
            let onComplete2:Function = function(){
                egret.Tween.get(self).to({
                    rotation:rotation2
                },2000).call(onComplete1,self);  
            }
            egret.Tween.get(self).to({
                rotation:rotation1
            },1500).call(onComplete2,self);  
        };
        onComplete1();
    }

    public setScale(scale:number)
    {
        this.scaleX = this.scaleY = scale;
    }

    public boom()
    {
        EffectUtil.instance().StopEffect(this);
        this.rotation = 0;
        this.img_fruit.visible = false;
        this.handleExperience();
        // this.img_dot.visible = true;
        FruitPool.pushFruit(this);
        SoundManager.instance().playImpactSound();
    }

    //process experience
    public handleExperience()
    {
        if(this.img_knife.visible)
        {
            egret.Tween.get(this.img_knife).to({
                scaleX:25,
                scaleY:25,
                alpha:0
            },500).set({
                visible:false,
                alpha:1,
                scaleX:1,
                scaleY:1
            })
            // this.img_knife.visible = false;
            GameManager.instance.addCurThreeTimes(3);
            MessageUtil.tips('三箭齐发体验!');
        }
        else if(this.img_dragon.visible)
        {
            egret.Tween.get(this.img_dragon).to({
                scaleX:25,
                scaleY:25,
                alpha:0
            },500).set({
                visible:false,
                alpha:1,
                scaleX:1.2,
                scaleY:1.2
            })
            // this.img_knife.visible = false;
            GameManager.instance.addCurDragonTimes(1);
            MessageUtil.tips('屠龙宝刀体验!');
        }
    }

    public killed()
    {
        this.rotation = 0;
        this.img_fruit.visible = false;
        SoundManager.instance().playImpactSound();
        FruitPool.pushFruit(this);
    }

    public reset()
    {
        // this.img_dot.visible = false;
    }
    
    public action()
    {
        this.speedY -= this.a;
        this.x += this.speedX ;
        this.y -= this.speedY ;
        this.rotation += this.rotationFlag;
    }

    public clear()
    {
        super.clear();
        EffectUtil.instance().StopEffect(this);
        this.speedY = null;
        this.speedX = null;
        this.rotationFlag = null;
        this.a = null;//加速度
    }
}